package com.ss.android.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.a.a.NightMode;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.R;

public final class AlertDialog extends Dialog implements DialogInterface {
    public static final class Builder {
        public final AlertController.AlertParams alertParams;

        public Builder(Context context) {
            this.alertParams = new AlertController.AlertParams(new ContextThemeWrapper(context, R.style.SSTheme_Dialog_Alert_Night));
        }

        public final Builder setCustomTitle(View view) {
            this.alertParams.mCustomTitleView = view;
            return this;
        }

        public final Builder setPositiveButton(int positiveResId, DialogInterface.OnClickListener positiveButtonListener) {
            this.alertParams.mPositiveButtonText = this.alertParams.mContext.getText(positiveResId);
            this.alertParams.mPositiveButtonListener = positiveButtonListener;
            return this;
        }

        public final AlertDialog create() {
            AlertDialog alertDialog = new AlertDialog(this.alertParams.mContext);
            AlertController.AlertParams alertParams = this.alertParams;
            AlertController alertController = alertDialog.mAlert;
            if (alertParams.mTitle != null) {
                alertController.setTitle(alertParams.mTitle);
            }

            if (alertParams.mMessage != null) {
                CharSequence v0 = alertParams.mMessage;
                alertController.mMessage = v0;
                if (alertController.mMessageView != null) {
                    alertController.mMessageView.setText(v0);
                }
            }

            if (alertParams.mPositiveButtonText != null) {
                alertController.setButton(-1, alertParams.mPositiveButtonText, alertParams.mPositiveButtonListener, null);
            }

            if (alertParams.mNegativeButtonText != null) {
                alertController.setButton(-2, alertParams.mNegativeButtonText, alertParams.mNegativeButtonListener, null);
            }

            if (alertParams.mNeutralButtonText != null) {
                alertController.setButton(-3, alertParams.mNeutralButtonText, alertParams.mNeutralButtonListener, null);
            }

            if (alertParams.mItems != null) {
                View view = alertParams.mInflater.inflate(alertController.mListLayout, null);
                int v1 = alertParams.mIsSingleChoice ? alertController.mMultiChoiceItemLayout : alertController.mSingleChoiceItemLayout;
                alertController.mAdapter = new AlertController.CheckedItemAdapter(alertParams.mContext, v1, alertParams.mItems);
                alertController.mCheckedItem = alertParams.mCheckedItem;
                if (alertParams.mOnItemSelectedListener != null) {
                    ((RecycleListView) view).setOnItemClickListener(new g(alertParams, alertController));
                }

                if (alertParams.mIsSingleChoice) {
                    ((RecycleListView) view).setChoiceMode(1);
                }

                alertController.mListView = ((ListView) view);
            }

            if (alertParams.mCustomTitleView != null) {
                alertController.mCustomTitleView = alertParams.mCustomTitleView;
            }

            alertDialog.setCancelable(this.alertParams.mCancelable);
            if (this.alertParams.mCancelable) {
                alertDialog.setCanceledOnTouchOutside(true);
            }

            alertDialog.setOnCancelListener(this.alertParams.mOnCancelListener);
            alertDialog.setOnDismissListener(null);
            return alertDialog;
        }

        public final Builder setTitle(int arg3) {
            this.alertParams.mTitle = this.alertParams.mContext.getText(arg3);
            return this;
        }

        public final Builder setPositiveButton(CharSequence arg2, DialogInterface.OnClickListener arg3) {
            this.alertParams.mPositiveButtonText = arg2;
            this.alertParams.mPositiveButtonListener = arg3;
            return this;
        }

        public final Builder setCancelable(boolean arg2) {
            this.alertParams.mCancelable = arg2;
            return this;
        }

        public final Builder setOnCancelListener(DialogInterface.OnCancelListener arg2) {
            this.alertParams.mOnCancelListener = arg2;
            return this;
        }

        public final Builder setOnItemSelectedListener(CharSequence[] arg2, DialogInterface.OnClickListener arg3) {
            this.alertParams.mItems = arg2;
            this.alertParams.mOnItemSelectedListener = arg3;
            return this;
        }

        public final Builder setSingleChoiceItems(int arg3, int arg4, DialogInterface.OnClickListener arg5) {
            this.alertParams.mItems = this.alertParams.mContext.getResources().getTextArray(arg3);
            this.alertParams.mOnItemSelectedListener = arg5;
            this.alertParams.mCheckedItem = arg4;
            this.alertParams.mIsSingleChoice = true;
            return this;
        }

        public final Builder setTitle(CharSequence arg2) {
            this.alertParams.mTitle = arg2;
            return this;
        }

        public final Builder setNegativeButton(int arg3, DialogInterface.OnClickListener arg4) {
            this.alertParams.mNegativeButtonText = this.alertParams.mContext.getText(arg3);
            this.alertParams.mNegativeButtonListener = arg4;
            return this;
        }

        public final Builder setMessage(CharSequence arg2) {
            this.alertParams.mMessage = arg2;
            return this;
        }

        public final Builder setMessage(int arg3) {
            this.alertParams.mMessage = this.alertParams.mContext.getText(arg3);
            return this;
        }

        public final Builder setNegativeButton(CharSequence arg2, DialogInterface.OnClickListener arg3) {
            this.alertParams.mNegativeButtonText = arg2;
            this.alertParams.mNegativeButtonListener = arg3;
            return this;
        }

        public final AlertDialog show() {
            AlertDialog v0 = this.create();
            try {
                v0.show();
            } catch (Exception v1) {
            }

            return v0;
        }
    }

    public AlertController mAlert;
    private static NightMode mNightMode;

    protected AlertDialog(Context arg4) {
        super(arg4, R.style.SSTheme_Dialog_Alert_Night);
        this.mAlert = new AlertController(this.getContext(), ((DialogInterface) this), this.getWindow());
        if (AlertDialog.mNightMode == null) {
            throw new IllegalArgumentException("NightMode is NUll");
        }
    }

    public static void setNightMode(NightMode arg0) {
        AlertDialog.mNightMode = arg0;
    }

    public final void setButton(int arg3, CharSequence arg4, DialogInterface.OnClickListener arg5) {
        this.mAlert.setButton(arg3, arg4, arg5, null);
    }

    protected final void onCreate(Bundle arg13) {
        int v5;
        View v2_1;
        View v1_1;
        int v2;
        super.onCreate(arg13);
        AlertController alertController = this.mAlert;
        alertController.mWindow.requestFeature(1);
        alertController.mWindow.setContentView(alertController.mAlertDialogLayout);
        View v0 = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.contentPanel);
        alertController.mScrollView = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.scrollView);
        alertController.mScrollView.setFocusable(false);
        alertController.mMessageView = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.message);
        if (alertController.mMessageView != null) {
            if (alertController.mMessage != null) {
                alertController.mMessageView.setText(alertController.mMessage);
            } else {
                alertController.mMessageView.setVisibility(View.GONE);
                alertController.mScrollView.removeView(alertController.mMessageView);
                if (alertController.mListView != null) {
                    ViewParent v1 = alertController.mScrollView.getParent();
                    v2 = ((ViewGroup) v1).indexOfChild(alertController.mScrollView);
                    ((ViewGroup) v1).removeViewAt(v2);
                    ((ViewGroup) v1).addView(alertController.mListView, v2, new ViewGroup.LayoutParams(-1, -1));
                } else {
                    v0.setVisibility(View.GONE);
                }
            }
            v1_1 = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.scrollIndicatorUp);
            v2_1 = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.scrollIndicatorDown);
            if (v1_1 == null && v2_1 == null) {
            } else if (alertController.mMessage != null) {
                alertController.mScrollView.post(new d(alertController, v1_1, v2_1));
            } else if (alertController.mListView != null) {
                alertController.mListView.setOnScrollListener(new e(v1_1, v2_1));
                alertController.mListView.post(new f(alertController, v1_1, v2_1));
            } else {
                if (v1_1 != null) {
                    ((ViewGroup) v0).removeView(v1_1);
                }

                if (v2_1 == null) {
                } else {
                    ((ViewGroup) v0).removeView(v2_1);
                }
            }
        }

        v2 = 0;
        alertController.mButtonPositive = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.button1);
        alertController.mButtonPositive.setOnClickListener(alertController.mButtonHandler);
        if (TextUtils.isEmpty(alertController.mButtonPositiveText)) {
            alertController.mButtonPositive.setVisibility(View.GONE);
        } else {
            alertController.mButtonPositive.setText(alertController.mButtonPositiveText);
            alertController.mButtonPositive.setVisibility(View.VISIBLE);
            v2 = 1;
        }

        alertController.mButtonNegative = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.button2);
        alertController.mButtonNegative.setOnClickListener(alertController.mButtonHandler);
        if (TextUtils.isEmpty(alertController.mButtonNegativeText)) {
            alertController.mButtonNegative.setVisibility(View.GONE);
        } else {
            alertController.mButtonNegative.setText(alertController.mButtonNegativeText);
            alertController.mButtonNegative.setVisibility(View.VISIBLE);
            v2 |= 2;
        }

        alertController.mButtonNeutral = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.button3);
        alertController.mButtonNeutral.setOnClickListener(alertController.mButtonHandler);
        if (TextUtils.isEmpty(alertController.mButtonNeutralText)) {
            alertController.mButtonNeutral.setVisibility(View.GONE);
        } else {
            alertController.mButtonNeutral.setText(alertController.mButtonNeutralText);
            alertController.mButtonNeutral.setVisibility(View.VISIBLE);
            v2 |= 4;
        }

        int v4 = v2 != 0 ? 1 : 0;
        v1_1 = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.topPanel);
        alertController.mIconView = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.icon);
        v2 = !TextUtils.isEmpty(alertController.mTitle) ? 1 : 0;
        if (v2 != 0) {
            alertController.mTitleView = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.alertTitle);
            alertController.mTitleView.setText(alertController.mTitle);
            alertController.mTitleView.setPadding(alertController.mIconView.getPaddingLeft(), alertController.mIconView.getPaddingTop(), alertController.mIconView.getPaddingRight(), alertController.mIconView.getPaddingBottom());
            alertController.mIconView.setVisibility(View.GONE);
            v5 = 1;
        } else {
            alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.title_template).setVisibility(View.GONE);
            alertController.mIconView.setVisibility(View.GONE);
            ((ViewGroup) v1_1).setVisibility(View.GONE);
            v5 = 0;
        }

        v2_1 = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.buttonPanel);
        if (v4 == 0) {
            v2_1.setVisibility(View.GONE);
            if (alertController.mMessage == null && alertController.mListView != null && alertController.mListView.getParent() != null) {
                alertController.mListView.setPadding(alertController.mListView.getPaddingLeft(), alertController.mListView.getPaddingTop(), alertController.mListView.getPaddingRight(), alertController.mListView.getPaddingTop());
            }
        }

        v2_1 = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.customPanel);
        View v7 = alertController.mCustomTitleView != null ? alertController.mCustomTitleView : null;
        int v6 = v7 != null ? 1 : 0;
        if (v6 == 0 || !AlertController.canTextInput(v7)) {
            alertController.mWindow.setFlags(131072, 131072);
        }

        if (v6 != 0) {
            ((ViewGroup) alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.custom)).addView(v7, new ViewGroup.LayoutParams(-1, -1));
            if (alertController.mListView != null) {
                ((LinearLayout.LayoutParams) v2_1.getLayoutParams()).weight = 0f;
            }
        } else {
            v2_1.setVisibility(View.GONE);
        }

        if (v5 != 0) {
            v2_1 = alertController.mMessage != null || v7 != null || alertController.mListView != null ? alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.c7) : alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.c8);
            if (v2_1 == null) {
            } else {
                v2_1.setVisibility(View.VISIBLE);
            }
        }
        int v0_1 = ((ViewGroup) v0).getVisibility() == View.VISIBLE ? 1 : 0;
        if (v4 != 0 && v5 != 0 && v6 == 0 && v0_1 == 0) {
            UIUtils.setLayoutMargins(v1_1, -3, -3, -3, ((int) UIUtils.dp2px(alertController.mContext, 16f)));
        }

        ListView v0_2 = alertController.mListView;
        if (v0_2 != null && alertController.mAdapter != null) {
            v0_2.setAdapter(alertController.mAdapter);
            int v1_2 = alertController.mCheckedItem;
            if (v1_2 >= 0) {
                v0_2.setItemChecked(v1_2, true);
                v0_2.setSelection(v1_2);
            }
        }

        if (Build.VERSION.SDK_INT >= 20) {
            v0 = alertController.mWindow.getDecorView();
            v1_1 = alertController.mWindow.findViewById(com.ss.android.article.browser.R.id.parentPanel);
            if (v1_1 != null && v0 != null) {
                v0.setOnApplyWindowInsetsListener(new com.ss.android.common.dialog.c(alertController, v1_1));
                v0.setFitsSystemWindows(true);
                v0.requestApplyInsets();
            }
        }
    }

    public final boolean onKeyDown(int arg4, KeyEvent arg5) {
        boolean v0 = true;
        AlertController v1 = this.mAlert;
        int v1_1 = v1.mScrollView == null || !v1.mScrollView.executeKeyEvent(arg5) ? 0 : 1;
        if (v1_1 == 0) {
            v0 = super.onKeyDown(arg4, arg5);
        }

        return v0;
    }

    public final boolean onKeyUp(int arg4, KeyEvent arg5) {
        boolean v0 = true;
        AlertController v1 = this.mAlert;
        int v1_1 = v1.mScrollView == null || !v1.mScrollView.executeKeyEvent(arg5) ? 0 : 1;
        if (v1_1 == 0) {
            v0 = super.onKeyUp(arg4, arg5);
        }

        return v0;
    }

    public final void setTitle(CharSequence arg2) {
        super.setTitle(arg2);
        this.mAlert.setTitle(arg2);
    }
}

